package org.oschina.api.user.role.service;

import java.util.List;

import org.oschina.Constants;
import org.oschina.Message;
import org.oschina.Module;
import org.oschina.ResultUtil;
import org.oschina.api.BaseService;
import org.oschina.bean.Result;
import org.oschina.security.annotation.Require;
import org.oschina.api.index.service.IndexService;
import org.oschina.api.user.role.dao.UserRoleTypeDAO;
import org.oschina.api.user.role.model.UserRoleType;
import org.oschina.api.user.role.vo.UserRoleTypeVo;
import org.oschina.api.user.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserRoleTypeService extends BaseService {

	@Autowired
	private UserRoleTypeDAO roleTypeDAO;
	@Autowired
	private IndexService indexService;
	@Autowired
	private UserService userService;
	
	/**
	 * 返回用户类型
	 * @return
	 */
	@Require(authorization={Module.MODULE_4,Module.MODULE_2,Module.MODULE_9,Module.MODULE_45,Module.MODULE_59},or=true)
	public List<UserRoleTypeVo> findVo()
	{
		return roleTypeDAO.findVo();
	}
	
	/**
	 * 设置默认
	 * @param id
	 * @return
	 */
	@Require(authorization=Module.MODULE_4)
	public Result setDefault(int id)
	{
		if(roleTypeDAO.setDefault(id)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(Message.UPDATE_SUCCESS);
	}
	
	/**
	 * 创建
	 * @param roleType
	 * @return
	 */
	@Require(authorization=Module.MODULE_4)
	public Result create(UserRoleType roleType)
	{
		roleType.setId(getIndex());
		roleType.set_default(false);
		roleTypeDAO.create(roleType);
		return ResultUtil.getSuccess(roleType);
	}
	
	/**
	 * 更新
	 * @param roleType
	 * @return
	 */
	@Require(authorization=Module.MODULE_4)
	public Result update(UserRoleType roleType)
	{
		if(roleTypeDAO.update(roleType)==0)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		return ResultUtil.getSuccess(roleType);
	}
	
	/**
	 * 删除
	 * @param id
	 * @param target
	 * @return
	 */
	@Require(authorization=Module.MODULE_4)
	public Result delete(int id,int target)
	{
		if(id==target)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		UserRoleTypeVo roleType=findById(id);
		if(roleType==null || roleType.get_default())
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		UserRoleTypeVo targetType=findById(target);
		if(targetType==null)
			return ResultUtil.getFailure(Message.ERROR_DATA_ERROR);
		
		_delete(id);
		userService._updateRoleByRole(id, target);
		return ResultUtil.getSuccess(Message.DELETE_SUCCESS);
	}
	
	private int _delete(int id)
	{
		return roleTypeDAO.delete(id);
	}
	
	/**
	 * 根据ID返回
	 * @param id
	 * @return
	 */
	public UserRoleTypeVo findById(int id)
	{
		return roleTypeDAO.findById(id);
	}
	
	/**
	 * 返回默认
	 * @return
	 */
	public UserRoleTypeVo getDefault()
	{
		return roleTypeDAO.getDefault();
	}
	
	private int getIndex()
	{
		return indexService.getMajorById(Constants.INDEX_USER_ROLE_TYPE);
	}
}